package com.lanou3g.baidumusic.main.mainactivity.addfragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.baidumusic.R;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/30.
 */

public class AddFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private AddFragmentBean bean;

    private static final int TYPE_TITLE = 0;

    public AddFragmentAdapter(Context context) {
        this.context = context;
    }

    public void setAddFragmentBean(AddFragmentBean bean) {
        this.bean = bean;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case TYPE_TITLE:
                View title = LayoutInflater.from(context).inflate(R.layout.item_fragment_add_rv_title, parent, false);
                viewHolder = new TitleHolder(title);
                break;
            default:
                View other = LayoutInflater.from(context).inflate(R.layout.item_fragment_add_rv_main,parent,false);
                viewHolder = new MainHolder(other);
                break;
        }


        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (position) {
            case TYPE_TITLE:
                TitleHolder titleHolder = (TitleHolder) holder;
                titleHolder.titleTv.setText(bean.getTitle());
                titleHolder.contentTv.setText(bean.getDesc());
                Picasso.with(context).load(bean.getPic_300()).into(titleHolder.imageView);
                break;
            default:
                MainHolder mainHolder = (MainHolder) holder;
                mainHolder.titleTv.setText(bean.getContent().get(position-1).getTitle());
                mainHolder.nameTv.setText(bean.getContent().get(position-1).getAuthor());
                break;
        }


    }

    @Override
    public int getItemCount() {

        if (null == bean) {
            return 0;
        }
        if (null == bean.getContent()) {
            return 0;
        }
        return bean != null && bean.getContent().size() > 0 ? bean.getContent().size() + 1 : 0;
    }


    @Override
    public int getItemViewType(int position) {

        switch (position) {
            case 0:
                return TYPE_TITLE;
            default:

                break;

        }

        return position+1;
    }

    public class TitleHolder extends RecyclerView.ViewHolder {

        private TextView titleTv, contentTv;
        private ImageView imageView;

        public TitleHolder(View itemView) {
            super(itemView);

            titleTv = (TextView) itemView.findViewById(R.id.item_fragment_add_rv_title_tv1);
            contentTv = (TextView) itemView.findViewById(R.id.item_fragment_add_rv_title_tv2);
            imageView = (ImageView) itemView.findViewById(R.id.item_fragment_add_rv_title_iv);

        }
    }

    public class MainHolder extends RecyclerView.ViewHolder {

        private TextView titleTv, nameTv;

        public MainHolder(View itemView) {
            super(itemView);

            titleTv = (TextView) itemView.findViewById(R.id.item_fragment_add_rv_main_tv1);
            nameTv = (TextView) itemView.findViewById(R.id.item_fragment_add_rv_main_tv2);

        }
    }


}
